//
//  Utils.m
//  EJournals
//
//  Created by pakeyfan on 12-7-1.
//  Copyright 2012 pakeyfan.personal. All rights reserved.
//

#import "Utils.h"


@implementation Utils

+ (void) writeConfigFile:(NSData *)data fromFile:(NSString *)strFile
{
	//Create File Manager
	NSFileManager *fileManager = [NSFileManager defaultManager];
	//Get Directory
	//Parameter NSDocumentDirectory dirctory type
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
														 NSUserDomainMask,
														 YES);
	//Get dir 
	NSString *documentsDirectory = [paths objectAtIndex:0];
	
	//Go dir
	[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
	 
	 //get file dir
	 NSString *path = [documentsDirectory stringByAppendingPathComponent:strFile];
	 
	 //check file exist
	 if(![[NSFileManager defaultManager] fileExistsAtPath: path])
	 {
		 [fileManager createFileAtPath:path contents:data
							attributes:nil];
	 }
}
	 
+ (NSMutableDictionary*) readConfigFile:(NSString*)file
{
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
														 NSUserDomainMask, 
														 YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
	
	 NSString *path = [documentsDirectory stringByAppendingPathComponent:file];
	 
	 NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
	 return dict;
}

+ (void) SetValueByKey:(NSString*)value modifyByValue:(NSString*)key fromFile:(NSString*)file
{
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
														 NSUserDomainMask, 
														 YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
	
	NSString *path = [documentsDirectory stringByAppendingPathComponent:file];
	
	NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
	[dict setObject:value forKey:key];
	NSData *data = [[NSData alloc] init];
	data = dict;
	[fileManager removeItemAtPath:path error:nil];
	[fileManager createFileAtPath:path contents:data attributes:nil];
	[data release];
}
	 
+ (NSString*) getValueByKey:(NSString*)key fromFile:(NSString*)file
{
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
														 NSUserDomainMask, 
														 YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];

	NSString *path = [documentsDirectory stringByAppendingPathComponent:file];
	
	NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
	
	NSString *object = [dict objectForKey:key];
	
	if(object == nil || object.length == 0)
	{
		object = @"0";
	}
	return object;
}
@end
